package com.example.mr5.money.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mr5.money.R;

import java.util.List;

/**
 * Created by Mr.5
 * Date：2016/9/25.
 * Time：18:27
 */
public class Segment extends LinearLayout implements View.OnClickListener {
    private List<String> titles;

    private OnSegmentSelectedListener onSegmentSelectedListener;
    private int defaultSelectedIndex = 0;
    private int selectedIndex = 0;

    public Segment(Context context) {
        super(context);
        setOrientation(HORIZONTAL);
    }

    public Segment(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
    }

    private void initView() {
        if (titles != null) {
            setWeightSum(titles.size());
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources()
                    .getDisplayMetrics());

            for (int i = 0; i < titles.size(); i++) {
                TextView radioButton = new TextView(getContext());
                radioButton.setPadding(10, 6, 10, 6);
                radioButton.setGravity(Gravity.CENTER);
                radioButton.setTextColor(getResources().getColorStateList(R.color.segment_text_selector));
                LayoutParams layoutParams = new LayoutParams(0, height, 1.0f);

                if (i == 0) {
                    radioButton.setBackgroundResource(R.drawable.segment_left_selector);
                } else if (i == titles.size() - 1) {
                    radioButton.setBackgroundResource(R.drawable.segment_right_selector);
                    layoutParams.leftMargin = -1;
                }
                radioButton.setLayoutParams(layoutParams);
                radioButton.setText(titles.get(i));
                addView(radioButton);
                radioButton.setTag(i);
                radioButton.setOnClickListener(this);
                if (i == defaultSelectedIndex){
                    radioButton.setSelected(true);
                    if (onSegmentSelectedListener != null){
                        onSegmentSelectedListener.onSegmentSelected(defaultSelectedIndex);
                    }
                }
            }
            postInvalidate();
        }
    }

    public List<String> getTitles() {
        return titles;
    }

    public String getTitle(int position) {
        return titles.get(position);
    }

    public void setTitles(List<String> titles) {
        this.titles = titles;
        initView();
    }

    public int getDefaultSelectedIndex() {
        return defaultSelectedIndex;
    }

    public void setDefaultSelectedIndex(int defaultSelectedIndex) {
        this.defaultSelectedIndex = defaultSelectedIndex;
        this.selectedIndex = defaultSelectedIndex;

    }


    @Override
    public void onClick(View v) {
        int index = (int) v.getTag();
        TextView button = (TextView) v;
        if (!button.isSelected()) {
            for (int i = 0; i < titles.size(); i++) {
                TextView btn = (TextView) getChildAt(i);
                btn.setSelected(false);
            }
            button.setSelected(true);
        }
        selectedIndex = index;
        if (onSegmentSelectedListener != null){
            onSegmentSelectedListener.onSegmentSelected(index);
        }

    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;
    }

    public OnSegmentSelectedListener getOnSegmentSelectedListener() {
        return onSegmentSelectedListener;
    }

    public void setOnSegmentSelectedListener(OnSegmentSelectedListener onSegmentSelectedListener) {
        this.onSegmentSelectedListener = onSegmentSelectedListener;
    }

    public interface OnSegmentSelectedListener{
        void onSegmentSelected(int index);
    }
}